import React, {Component} from 'react'
import { withRouter } from 'react-router-dom';
import { Toast, Button, Icon, Tools,Modal } from "weaver-mobile";
import '../../../../style/qrLogin.less';
const getLabel = Tools.getLabel;
const {alert} = Modal ;

@withRouter
export default class TestQRLoginCa extends Component {


  constructor(props) {
    super(props);
    this.state = {
      loginkey: '',
      supportca: '',
      operateflag: '',
      logindisable : true
    }
  }
  // 是否开启CA扫码登录
  isCaOPen = false

  componentDidMount(){
    Tools.callApi({url:'/api/hrm/login/qrcode/isCaOPen'}).then( data=>{
      this.isCaOPen=data.isCaOPen ;
      let search = this.props.location.search;
      const obj = {};
      if(search.trim().length > 0){
        search = search.substring(1);
        const params = search.split('&');
        params.map(param => {
          const kv = param.split('=');
          Object.assign(obj, {
            [kv[0]]: kv[1]
          });
        });
      }
      this.setState({'logindisable':false,...obj})
    }).catch((res)=>{})
  }

  onConfirm = () => {
      this.checkNeedCaAndInvoke(this.state ,(afterInvokeParams) => {
          Tools.callApi({url: '/api/hrm/login/qrcode/loginQRCode', method: 'POST', params: afterInvokeParams}).then(data => {
            const loginStatus = data['loginStatus']
            if('1' == loginStatus){
              this.onCancel();
            }else{
              alert(getLabel('25700','错误信息'),'error:'+loginStatus+";msg:"+data['message'])
            }
          }).catch((res) => {});
      })
  }

  
  checkNeedCaAndInvoke=(params,callback)=>{
   let afterInvokeParams ={...params}
    if(this.isCaOPen){
      this.invokeCaSignValueNoPassForJsApi(afterInvokeParams,(signInfo)=>{
          alert(getLabel('25700','错误信息'),'invokeCaSignValueNoPassForJsApi begin') ;
          const errCode = signInfo['errCode']
          const errMsg = signInfo['errMsg']
          if(errCode =='0'){
            //success
            const {certInfo} = signInfo
            afterInvokeParams['cert'] = certInfo['cert'] 
            
            afterInvokeParams['signAlg'] = certInfo['signAlg']
            afterInvokeParams['signatureValue'] = certInfo['signValue']
            afterInvokeParams['isSigned']='1'
            callback(afterInvokeParams)
          }else{
            alert(getLabel('25700','错误信息'),'error:'+errCode+';msg:'+errMsg)
          }
      })
    }else{
        callback(afterInvokeParams)
    }
  }

  invokeCaSignValueNoPassForJsApi = (params,callback) => {
      alert(getLabel('25700','错误信息'),'begin invoke ca') ;
      const em = window.em ;
      // 1 check jsapi active

      alert(getLabel('25700','错误信息'),'em check ok') ;
      if(!em){
        alert(getLabel('25700','错误信息'),'error:-900') ;
        return ;
      }
      alert(getLabel('25700','错误信息'),'begin CAMethod') ;
      if(em.CAMethod){
          alert(getLabel('25700','错误信息'),'CAMethod check ok') ;
          em.CAMethod({
              func:'getSignValueNoPass',
              original:params['randomNumber'],
              success : callback,
              callback: 'certInfo'
          })
          alert(getLabel('25700','错误信息'),'after invoke CAMethod use getSignValueNoPass') ;
      }else{
        alert(getLabel('25700','错误信息'),'error:-902')
        return
      }
      
     // em.getSignValueNoPass({'original':params['randomNumber']},callback);
  }





  onCancel = () => {
    if(window.em){
      window.em.closeWindow();
    }
  }

  render() {
    return (
      <div className='qrLogin'>
        <div className='title'>
          <div>
            <img src='/hrm/hrm_e9/image/pc.png' />
            <div className='tip'>{getLabel('390249',"PC版登录确认")}</div>
          </div>
        </div>
        <div className='btnGroup'>
          <Button ref="login" disabled={this.state.logindisable} type='primary' className='login' onClick={this.onConfirm}>{getLabel('674',"登录")}</Button>
          <Button type='default' className='cancel' onClick={this.onCancel}>{getLabel('390291',"取消登录")}</Button>
        </div>
      </div>
    )
  }
}